package com.coocaa.typhoon.api.v1.userfile.provider;

import cn.hutool.core.io.file.FileNameUtil;
import com.coocaa.cirrus.common.third.filestore.config.TosProperties;
import com.coocaa.typhoon.api.third.volcengine.service.TOSService;
import com.coocaa.typhoon.api.v1.userfile.constant.FileSceneConstant;
import com.coocaa.typhoon.api.v1.userfile.model.UploadTmpResult;
import com.coocaa.typhoon.api.v1.userfile.util.TosFinalUrlUtil;
import com.coocaa.typhoon.api.v1.userfile.util.TosUploadFilePathUtil;
import com.drew.imaging.FileType;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author bijiahao
 * @date : 2025/9/26 18:31
 * @description
 */
@Service
public class PptFileUploadProvider  {
    private static final int DPI = 150;               // 清晰度，150 足够预览
    private static final Float COMPRESS_QUALITY = 0.85f;   // 图片质量 85%
    @Autowired
    private TOSService tosService;
    @Autowired
    private TosProperties tosProperties;

    private static final String FONT_PATH = "fonts/HaiPaiQiangDiaoGunShiChaoHeiJian-2.ttf";


    public List<UploadTmpResult> splitUploadImage(InputStream inputStream, UploadTmpResult uploadTmpResult) throws IOException {
        List<UploadTmpResult> res = new ArrayList<>();
        XMLSlideShow ppt = new XMLSlideShow(inputStream);

        // 计算缩放（默认 72 dpi → 目标 dp
        double scale = DPI / 72.0;
        Dimension pgsize = ppt.getPageSize();
        int width = (int) (pgsize.width * scale);
        int height = (int) (pgsize.height * scale);
        List<XSLFSlide> slides = ppt.getSlides();
        for (int i = 0; i < slides.size(); i++) {
            BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.setPaint(Color.WHITE);
            graphics.fill(new Rectangle2D.Float(0, 0, width, height));
            graphics.scale(scale, scale);
            slides.get(i).draw(graphics);   // 把幻灯片画进 BufferedImage
            graphics.dispose();
            // 压缩
            ByteArrayOutputStream imgOut = new ByteArrayOutputStream();
            String extName = FileType.Png.getName().toLowerCase();
            Thumbnails.of(img)
                    .scale(1.0)              // 不缩放
                    .outputQuality(COMPRESS_QUALITY)
                    .outputFormat(extName)
                    .toOutputStream(imgOut);
            String remoteUploadFilePath = TosUploadFilePathUtil.getRemoteUploadFilePathForRlsFile(tosProperties.getTosEnvDir(), uploadTmpResult.getFileName(), i, extName, FileSceneConstant.PREVIEW);
            tosService.uploadFile(imgOut, remoteUploadFilePath);
            UploadTmpResult tmpResult = UploadTmpResult.builder()
                    .fileExtName(extName.toUpperCase())
                    .fileName(FileNameUtil.getName(remoteUploadFilePath))
                    .tosLink(TosFinalUrlUtil.getTosLink(tosProperties.getTosBucketName(), remoteUploadFilePath))
                    .url(TosFinalUrlUtil.getHttpUrl(tosProperties.getTosUrlPrefix(), remoteUploadFilePath))
                    .build();
            res.add(tmpResult);
        }
        return res;
    }
}
